﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

using log4net;

namespace FSViet.Survey.Administration
{
	public partial class Main : System.Web.UI.MasterPage
	{
		ILog _logger = LogManager.GetLogger("RollingFile");
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				if (Session["SiteId"] == null && !Page.User.IsInRole("System administrators"))
					CheckUser();
			}
		}

		void CheckUser()
		{
			Helpers.DataHelper dataH = new Helpers.DataHelper();
			try
			{
				var data = dataH.ExecuteScalar("SELECT SiteId FROM UserSiteMappings WHERE UserId='" + Membership.GetUser().ProviderUserKey.ToString() + "'");
				Session["SiteId"] =  (Guid)new Guid(data.ToString());
			}
			catch (Exception ex)
			{
				_logger.Error("Error when checking user on site.",ex);
				Response.Redirect("~/Error.aspx?msg=Access denied.");
			}
		}
	}
}